package org.tis.tools.abf.module.om.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.ac.controller.request.AcMenuMoveRequest;
import org.tis.tools.abf.module.om.controller.request.OmOrgAddRequest;
import org.tis.tools.abf.module.om.controller.request.OmOrgSetDateRequest;
import org.tis.tools.abf.module.om.controller.request.OmOrgUpdateRequest;
import org.tis.tools.abf.module.om.controller.request.OmTreeQueryParameterRequest;
import org.tis.tools.abf.module.om.entity.OmOrg;
import org.tis.tools.abf.module.om.entity.enums.OmOrgStatus;
import org.tis.tools.abf.module.om.service.IOmOrgService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * describe: 机构controller
 *
 * @author zhaoch
 * @date 2018/3/27
**/
@RestController
@RequestMapping("/omOrg")
@Validated
@Slf4j
public class OmOrgController extends BaseController<OmOrg> {

    @Autowired
    private IOmOrgService orgService;


    @ApiOperation(value = "新增根机构",notes = "传入OmOrgAddRequest类作为参数")
    @OperateLog(type = OperateType.ADD, desc = "新增根机构")
    @PostMapping("/addRoot")
    public ResultVO addRoot(@RequestBody @Validated @ApiParam(value = "OmOrgAddRequest",name = "传入的OmOrgAddRequest类", required = true) OmOrgAddRequest request) {
            orgService.createRootOrg(request.getArea(), request.getOrgDegree(),request.getOrgName(), request.getOrgType(),request.getOrgAddr(),request.getLinkMan(),request.getLinkTel(),request.getStartDate(),request.getEndDate(),request.getRemark());
        return ResultVO.success("新增成功！");
    }


    @ApiOperation(value = "新增子机构",notes = "传入OmOrgAddRequest类作为参数")
    @OperateLog(type = OperateType.ADD, desc = "新增子机构")
    @PostMapping("/addChild")
    public ResultVO addChild(@RequestBody @Validated @ApiParam(value = "OmOrgAddRequest",name = "传入的OmOrgAddRequest类", required = true) OmOrgAddRequest request) {
        orgService.createChildOrg(request.getArea(), request.getOrgDegree(),request.getOrgName(), request.getOrgType(), request.getGuidParents(),request.getOrgAddr(),request.getLinkMan(),request.getLinkTel(),request.getStartDate(),request.getEndDate(),request.getRemark());
        return ResultVO.success("新增成功！");
    }


    @ApiOperation(value = "修改机构",notes = "传入OmOrgUpdateRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "修改机构")
    @PutMapping
    public ResultVO updateRoot(@RequestBody @Validated @ApiParam(value = "OmOrgUpdateRequest",name = "传入的OmOrgUpdateRequest类", required = true) OmOrgUpdateRequest omOrgUpdateRequest){
        OmOrg omOrg = orgService.selectById(omOrgUpdateRequest.getGuid());
        if (null == omOrg){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        boolean isexist = orgService.changeOrg(omOrgUpdateRequest);
        if (!isexist){
            return ResultVO.error("404","机构代码已存在,请重新输入!");
        }

        OmOrg omOrgquery = orgService.selectById(omOrgUpdateRequest.getGuid());
        return ResultVO.success("修改成功！",omOrgquery);
    }


    @ApiOperation(value = "删除机构",notes = "传入机构GUID(id)作为参数")
    @OperateLog(type = OperateType.DELETE, desc = "删除机构")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "机构GUID", required = true) String id) {
        OmOrg omOrg = orgService.selectById(id);
        if (omOrg == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        orgService.delectRoot(id);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "根据ID查询机构",notes = "传入机构GUID(id)作为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "机构GUID", required = true) String id) {
        OmOrg omOrg = orgService.selectById(id);
        if (omOrg == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", omOrg);
    }


    @ApiOperation(value = "查询机构列表",notes = "传入分页信息作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息", required = true) SmartPage<OmOrg> page) {
        return  ResultVO.success("查询成功", orgService.selectPage(page.getPageObj(), page.getConditionWrapper()));
    }


    @ApiOperation(value = "查询所有机构",notes = "无需传入参数")
    @GetMapping("/queryAll")
    public ResultVO queryAllOrg(){
        return ResultVO.success("查询成功",orgService.queryAllOrg());
    }


    @ApiOperation(value = "级联查询所有机构",notes = "无需传入参数")
    @GetMapping("/cascadeQueryAll")
    public ResultVO cascadeQueryAll(){
        return ResultVO.success("查询成功",orgService.cascadeQueryAll());
    }


    @ApiOperation(value = "查询机构的树结构",notes = "无需传入参数")
    @GetMapping("/tree")
    public ResultVO tree(){
        return ResultVO.success("查询成功！",orgService.queryOrgTree());
    }


    @ApiOperation(value = "查询机构名称机构的树结构",notes = "传入OmTreeQueryParameterRequest作为参数")
    @PostMapping("/orgTreeFilter")
    public ResultVO orgTreeFilter(@RequestBody @Validated @ApiParam(value = "name",name = "机构名称", required = true) OmTreeQueryParameterRequest request){
        return ResultVO.success("查询成功！",orgService.queryOrgTreeFilter(request.getName()));
    }


    @ApiOperation(value = "设置机构有效时间",notes = "传入OmOrgSetDateRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "设置机构的有效时间")
    @PutMapping("/setDate")
    public ResultVO setDate(@RequestBody @Validated @ApiParam(value = "OmOrgSetDateRequest",name = "传入的OmOrgSetDateRequest类", required = true) OmOrgSetDateRequest omOrgSetDateRequest){
        OmOrg omOrg = orgService.selectById(omOrgSetDateRequest.getGuid());
        if (null == omOrg){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        omOrg.setStartDate(omOrgSetDateRequest.getStartDate());
        omOrg.setEndDate(omOrgSetDateRequest.getEndDate());
        orgService.updateById(omOrg);

        return ResultVO.success("设置成功！",omOrg);
    }


    @ApiOperation(value = "注销机构",notes = "传入机构GUID(id)作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "注销机构")
    @PutMapping("/cancelStatus/{id}")
    public ResultVO cancelStatus(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "机构GUID", required = true) String id){
        OmOrg omOrg = orgService.selectById(id);
        if (null == omOrg){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (OmOrgStatus.RUNNING != omOrg.getOrgStatus()){
            return ResultVO.error("404", "只有机构状态为正常时才可以注销！");
        }

        omOrg.setOrgStatus(OmOrgStatus.CANCEL);
        orgService.updateById(omOrg);

        return ResultVO.success("注销成功！",omOrg);
    }


    @ApiOperation(value = "停用机构",notes = "传入机构GUID(id)作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "停用机构")
    @PutMapping("/stopStatus/{id}")
    public ResultVO stopStatus(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "机构GUID", required = true) String id){
        OmOrg omOrg = orgService.selectById(id);
        if (null == omOrg){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (OmOrgStatus.CANCEL != omOrg.getOrgStatus()){
            return ResultVO.error("404", "只有机构状态为注销时才可以停用！");
        }

        omOrg.setOrgStatus(OmOrgStatus.STOP);
        orgService.updateById(omOrg);

        return ResultVO.success("停用成功！",omOrg);
    }


    @ApiOperation(value = "启用机构",notes = "传入机构GUID(id)作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "启用机构")
    @PutMapping("/runningStatus/{id}")
    public ResultVO runningStatus(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "机构GUID", required = true) String id){
        OmOrg omOrg = orgService.selectById(id);
        if (null == omOrg){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        omOrg.setOrgStatus(OmOrgStatus.RUNNING);
        orgService.updateById(omOrg);

        return ResultVO.success("启用成功！",omOrg);
    }

    @ApiOperation(value = "移动机构",notes = "传入AcMenuMoveRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE, desc = "修改机构（移动）")
    @PostMapping("/queryMoveOrgLists")
    public ResultVO getMoveOrg(@RequestBody @Validated @ApiParam(value = "AcMenuMoveRequest",name = "传入的AcMenuMoveRequest类",required = true) AcMenuMoveRequest acMenuMoveRequest){
          OmOrg omOrg = orgService.moveOrg(acMenuMoveRequest.getTargetGuid(),acMenuMoveRequest.getMoveGuid(),
                  acMenuMoveRequest.getOrder());
        return ResultVO.success("查询成功",omOrg);
    }

}
